import cv2 as cv
import sys
import os
from python_ai.common.xcommon import *
import matplotlib.pyplot as plt
import numpy as np
import time
import datetime


def my_show_img(img, title="no title", trans=None, **kwargs):
    global spn
    spn += 1
    plt.subplot(spr, spc, spn)
    if trans is not None:
        img = trans(img)
    plt.imshow(img, **kwargs)
    plt.axis('off')
    plt.title(title)


img_dir = '../../../../large_data/pic'
img_name = 'friends.png'
img_path1 = os.path.join(img_dir, img_name)

spr = 2
spc = 3
spn = 0
plt.figure(figsize=[12, 6])

sep('load')
img = cv.imread(img_path1, cv.IMREAD_COLOR)
print('original shape', img.shape)
H, W, _ = img.shape
H2 = (H - 1) // 2
W2 = (W - 1) // 2
my_show_img(img, 'original', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

M = cv.getRotationMatrix2D((0, 0), 30, 1)
dst = cv.warpAffine(img, M, (W, H))
my_show_img(dst, 'rotate 0 0 1', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

M = cv.getRotationMatrix2D((W2, H2), 30, 1)
dst = cv.warpAffine(img, M, (W, H))
my_show_img(dst, 'rotate center 1', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

M = cv.getRotationMatrix2D((0, 0), 30, 2)
dst = cv.warpAffine(img, M, (W, H))
my_show_img(dst, 'rotate 0 0 2', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

M = cv.getRotationMatrix2D((W2, H2), 30, 2)
dst = cv.warpAffine(img, M, (W, H))
my_show_img(dst, 'rotate center 2', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

M = cv.getRotationMatrix2D((W2, H2), 180, 1)
dst = cv.warpAffine(img, M, (W, H))
my_show_img(dst, '180', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))
